package cn.edu.scau.cmi.chenenting.client;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.chenenting.dao.*;
import cn.edu.scau.cmi.chenenting.domain.*;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class Client extends Application {
	
	Stage stage = new Stage();
	Stage dialogStage = new Stage();
	
	public void start(Stage primaryStage) {
		Pane pane = new Pane();
		
		Button btnStudentAdd_1 = new Button("Student Add");
		btnStudentAdd_1.setLayoutX(220);
		btnStudentAdd_1.setLayoutY(14);
		btnStudentAdd_1.setOnAction(e->{
			function_1();
		});
		Button btnStudentModify_2 = new Button("Student Modify");
		btnStudentModify_2.setLayoutX(213);
		btnStudentModify_2.setLayoutY(49);
		btnStudentModify_2.setOnAction(e->{
			function_2();
		});
		Button btnStudentDelete_3 = new Button("Student Delete");
		btnStudentDelete_3.setLayoutX(215);
		btnStudentDelete_3.setLayoutY(84);
		btnStudentDelete_3.setOnAction(e->{
			function_3();
		});
		Button btnTeacherAdd_4 = new Button("Teacher Add");
		btnTeacherAdd_4.setLayoutX(221);
		btnTeacherAdd_4.setLayoutY(119);
		btnTeacherAdd_4.setOnAction(e->{
			function_4();
		});
		Button btnTeacherModify_5 = new Button("Teacher Modify");
		btnTeacherModify_5.setLayoutX(214);
		btnTeacherModify_5.setLayoutY(154);
		btnTeacherModify_5.setOnAction(e->{
			function_5();
		});
		Button btnTeacherDelete_6 = new Button("Teacher Delete");
		btnTeacherDelete_6.setLayoutX(216);
		btnTeacherDelete_6.setLayoutY(189);
		btnTeacherDelete_6.setOnAction(e->{
			function_6();
		});
		Button btnChooseTutor_7 = new Button("Student Choose Tutor");
		btnChooseTutor_7.setLayoutX(198);
		btnChooseTutor_7.setLayoutY(224);
		btnChooseTutor_7.setOnAction(e->{
			function_7();
		});
		Button btnModifyTutor_8 = new Button("Student Modify Tutor");
		btnModifyTutor_8.setLayoutX(199);
		btnModifyTutor_8.setLayoutY(259);
		btnModifyTutor_8.setOnAction(e->{
			function_8();
		});
		Button btnChooseStudent_9 = new Button("Tutor Choose Student");
		btnChooseStudent_9.setLayoutX(198);
		btnChooseStudent_9.setLayoutY(294);
		btnChooseStudent_9.setOnAction(e->{
			function_9();
		});
		Button btnModifyStudent_10 = new Button("Tutor Modify Student");
		btnModifyStudent_10.setLayoutX(199);
		btnModifyStudent_10.setLayoutY(329);
		btnModifyStudent_10.setOnAction(e->{
			function_10();
		});
		Button btnExit = new Button("Exit");
		btnExit.setLayoutX(250);
		btnExit.setLayoutY(364);
		btnExit.setOnAction(e->{
			stage.close();
		});
		
		pane.getChildren().add(btnStudentAdd_1);
		pane.getChildren().add(btnStudentModify_2);
		pane.getChildren().add(btnStudentDelete_3);
		pane.getChildren().add(btnTeacherAdd_4);
		pane.getChildren().add(btnTeacherModify_5);
		pane.getChildren().add(btnTeacherDelete_6);
		pane.getChildren().add(btnChooseTutor_7);
		pane.getChildren().add(btnModifyTutor_8);
		pane.getChildren().add(btnChooseStudent_9);
		pane.getChildren().add(btnModifyStudent_10);
		pane.getChildren().add(btnExit);
		
		Scene scene = new Scene(pane, 540, 400);
		stage.setTitle("Hibernate");
		stage.setScene(scene);
		stage.show();
	}
	
	public void function_1() {
		Pane pane = new Pane();
		
		Label lblID = new Label("Student ID:");
		lblID.setLayoutX(40);
		lblID.setLayoutY(40);
		Label lblName = new Label("Student Name:");
		lblName.setLayoutX(40);
		lblName.setLayoutY(70);
		
		TextField txtID = new TextField();
		txtID.setLayoutX(135);
		txtID.setLayoutY(36);
		TextField txtName = new TextField();
		txtName.setLayoutX(135);
		txtName.setLayoutY(66);

		Button btnAdd = new Button("Add");
		btnAdd.setLayoutX(130);
		btnAdd.setLayoutY(105);
		btnAdd.setOnAction(e->{
			JFrame jf = new JFrame();
			if (txtName.getText().isEmpty()) {
				JOptionPane.showMessageDialog(jf, "Please enter the student's name!", 
						"", JOptionPane.INFORMATION_MESSAGE);
			} else {
				Student student = new Student();
				student.setId(Long.parseLong(txtID.getText()));
				student.setName(txtName.getText());
				Session session = HibernateSessionFactoryUtil.getSession();
				Transaction transaction = session.beginTransaction();
				new StudentDAO().save(student);
				transaction.commit();
				session.close();
				JOptionPane.showMessageDialog(jf, "Successfully!", "", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		Button btnExit = new Button("Exit");
		btnExit.setLayoutX(180);
		btnExit.setLayoutY(105);
		btnExit.setOnAction(e->{
			dialogStage.close();
		});
		
		pane.getChildren().add(lblID);
		pane.getChildren().add(lblName);
		pane.getChildren().add(txtID);
		pane.getChildren().add(txtName);
		pane.getChildren().add(btnAdd);
		pane.getChildren().add(btnExit);
		
		Scene scene = new Scene(pane, 340, 150);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_2() {
		Pane pane = new Pane();
		
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_3() {
		Pane pane = new Pane();
		
		Label lblStudentID = new Label("Student ID:");
		lblStudentID.setLayoutX(40);
		lblStudentID.setLayoutY(40);
		
		TextField txtID = new TextField();
		txtID.setLayoutX(120);
		txtID.setLayoutY(36);
		
		Button btnSearch = new Button("Search");
		btnSearch.setLayoutX(291);
		btnSearch.setLayoutY(36);
		btnSearch.setOnAction(e->{
			if (!txtID.getText().isEmpty()) {
				final Student student = new StudentDAO().findById(Long.parseLong(txtID.getText()));
				
				Label lblID = new Label("ID");
				lblID.setLayoutX(80);
				lblID.setLayoutY(80);
				Label lblName = new Label("name");
				lblName.setLayoutX(170);
				lblName.setLayoutY(80);
				Label lblShowID = new Label(student.getId().toString());
				lblShowID.setLayoutX(80);
				lblShowID.setLayoutY(100);
				Label lblShowName = new Label(student.getName());
				lblShowName.setLayoutX(170);
				lblShowName.setLayoutY(100);
				
				pane.getChildren().add(lblID);
				pane.getChildren().add(lblName);
				pane.getChildren().add(lblShowID);
				pane.getChildren().add(lblShowName);
				
			}
			
		});
		Button btnDelete = new Button("Delete");
		btnDelete.setLayoutX(120);
		btnDelete.setLayoutY(150);
		btnDelete.setOnAction(e->{
			final Student student = new StudentDAO().findById(Long.parseLong(txtID.getText()));
			Session session = HibernateSessionFactoryUtil.getSession();
			Transaction transaction = session.beginTransaction();
			new StudentDAO().delete(student);
			transaction.commit();
			session.close();
			JFrame jf = new JFrame();
			JOptionPane.showMessageDialog(jf, "Successfully!", "", JOptionPane.INFORMATION_MESSAGE);
		});
		Button btnExit = new Button("Exit");
		btnExit.setLayoutX(200);
		btnExit.setLayoutY(150);
		btnExit.setOnAction(e->{
			dialogStage.close();
		});
		
		pane.getChildren().add(lblStudentID);
		pane.getChildren().add(txtID);
		pane.getChildren().add(btnSearch);
		pane.getChildren().add(btnDelete);
		pane.getChildren().add(btnExit);
		
		Scene scene = new Scene(pane, 380, 200);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_4() {
		Pane pane = new Pane();
		
		Label lblID = new Label("Teacher ID:");
		lblID.setLayoutX(40);
		lblID.setLayoutY(40);
		Label lblName = new Label("Teacher Name:");
		lblName.setLayoutX(40);
		lblName.setLayoutY(70);
		
		TextField txtID = new TextField();
		txtID.setLayoutX(135);
		txtID.setLayoutY(36);
		TextField txtName = new TextField();
		txtName.setLayoutX(135);
		txtName.setLayoutY(66);

		Button btnAdd = new Button("Add");
		btnAdd.setLayoutX(130);
		btnAdd.setLayoutY(105);
		btnAdd.setOnAction(e->{
			JFrame jf = new JFrame();
			if (txtName.getText().isEmpty()) {
				JOptionPane.showMessageDialog(jf, "Please enter the teacher's name!", 
						"", JOptionPane.INFORMATION_MESSAGE);
			} else {
				Teacher teacher = new Teacher();
				teacher.setId(Long.parseLong(txtID.getText()));
				teacher.setName(txtName.getText());
				Session session = HibernateSessionFactoryUtil.getSession();
				Transaction transaction = session.beginTransaction();
				new TeacherDAO().save(teacher);
				transaction.commit();
				session.close();
				JOptionPane.showMessageDialog(jf, "Successfully!", "", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		Button btnExit = new Button("Exit");
		btnExit.setLayoutX(180);
		btnExit.setLayoutY(105);
		btnExit.setOnAction(e->{
			dialogStage.close();
		});
		
		pane.getChildren().add(lblID);
		pane.getChildren().add(lblName);
		pane.getChildren().add(txtID);
		pane.getChildren().add(txtName);
		pane.getChildren().add(btnAdd);
		pane.getChildren().add(btnExit);
		
		Scene scene = new Scene(pane, 340, 150);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_5() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_6() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_7() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_8() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_9() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
	
	public void function_10() {
		Pane pane = new Pane();
		
		
		Scene scene = new Scene(pane, 540, 400);
		dialogStage.setScene(scene);
		dialogStage.show();
	}
}
